package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookChapterModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.service.WwdxBookChapterServiceI;
import com.wwdx.utils.ConstantHolder;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxBookChapterServiceImpl extends BaseServiceImpl<WwdxBookChapterModel> implements WwdxBookChapterServiceI {

    @Override
    public void bookchapterStatistics(Long chapterid){
        logger.info("章节ID,{}", chapterid);
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("chapterid",chapterid);
        String hql = "from WwdxBookChapterModel where chapterid=:chapterid";
        WwdxBookChapterModel wwdxBookChapaterModel = getByHql(hql,params);
        Long clickCount = wwdxBookChapaterModel.getClick_count(),virtualClickCount = wwdxBookChapaterModel.getVirtual_click_count();
        if (clickCount == null){
            clickCount = 0L;
        }
        if (virtualClickCount == null){
            virtualClickCount = 1000L;
        }
        clickCount ++;
        virtualClickCount += RandomUtils.nextInt(1, 4);

        wwdxBookChapaterModel.setClick_count(clickCount);
        wwdxBookChapaterModel.setVirtual_click_count(virtualClickCount);
        wwdxBookChapaterModel.setGmt_modified(new Date());
        super.update(wwdxBookChapaterModel);
    }

    @Override
    public List<WwdxBookChapterModel> findBookChapterList(Long bookid){
        String key = String.format(ConstantHolder.BOOK_CHAPTER_LIST, bookid);
        List<WwdxBookChapterModel> bookChapterList = (List<WwdxBookChapterModel>) valueOperations.get(key);
        if (CollectionUtils.isEmpty(bookChapterList)){
            Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
            params.put("bookid",bookid);
            String hql = "from WwdxBookChapterModel where bookid=:bookid order by sort";
            bookChapterList = super.find(hql, params);
            if (!CollectionUtils.isEmpty(bookChapterList)){
                valueOperations.set(key,bookChapterList);
            }
        }
        return bookChapterList;
    }

    @Override
    public List<WwdxBookChapterModel> findBookChapterList(Long bookid, String unionid) {
        WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(bookid);
        Integer isOrder = wwdxOrderService.isorder(bookid, unionid);
        String bookimg = wwdxBookModel.getBookimg(),bookname = wwdxBookModel.getBookname();
        List<WwdxBookChapterModel> bookChapterList = this.findBookChapterList(bookid);
        bookChapterList.forEach(wwdxBookChapaterModel -> {
            wwdxBookChapaterModel.setBookimg(bookimg);
            wwdxBookChapaterModel.setIsorder(isOrder);
            wwdxBookChapaterModel.setBookname(bookname);
        });
        return bookChapterList;
    }
}